<?php defined('SYSPATH') or die('No direct script access.');

// URL: /<context>/index.php/Home/
class Controller_Search extends Controller_Base 
{
	private $categoryService;
	private $projectService;
	private $searchService;
	
	public function __construct(Request $request, Response $response)
	{
		parent::__construct($request, $response);
		
		$this->categoryService = new Service_GenericSeedniaServiceStub(WSROOT.WSCONTEXT.'/'.SERVICE_CATEGORY.'/', ACCESS_TOKEN);
		$this->categoryService->init($this->_requestContext);

		$this->projectService = new Service_GenericSeedniaServiceStub(WSROOT.WSCONTEXT.'/'.SERVICE_PROJECT.'/', ACCESS_TOKEN);
		$this->projectService->init($this->_requestContext);

		$this->searchService = new Service_GenericSeedniaServiceStub(WSROOT.WSCONTEXT.'/'.SERVICE_SEARCH.'/', ACCESS_TOKEN);
		$this->searchService->init($this->_requestContext);

	}
	
	private function loadDefaultContent(&$template)
	{
		$ref_types = array('People' => 'Profile', 'Goals' => 'Project', 'Posts' => 'Post');
		$template->set_global('ref_types', $ref_types);
	}
	
	public function after()
	{
		$this->loadDefaultContent($this->template);
		parent::after();
	}
	
	public function action_index()
	{
		$contentFrame = $this->index('Seednia::Search');
		$content = $this->createView('search_content');
		$contentFrame->content = $content;

		try {
			//$profile = $this->_profileHelper->loadProfile($this->template, 9); // 1->tags + 8->relations
			$this->loadDefaultContent($this->template);

			$type = $this->request->query('type');
			$text = $this->request->query('searchText');
			if ($text == null)
				$text = $this->request->post('searchText');

			$queryParams = array('offset' => 0, 'max' => 10
				, '_commandSuffix' => '-by_user'
				, 'userSid' => (int)$this->_sessionInfo->userInfo['userSid'] );
			
			$searchParam = array('text'=>$text, 'params' => json_encode($queryParams));
			$content->hitsByType = $this->searchService->get($searchParam);
			$this->template->content = $content;

		} catch (Exception $ex)
		{
			Kohana_Log::instance()->add(Kohana_Log::ERROR, 'Exception::Add action_index: ' . $ex->getMessage());
		}

	}
	
	/**
	 * Search on profile 
	 */
	public function action_profile()
	{
		$contentFrame = $this->index('Seednia::Search');
		$content = $this->createView('search_profile');
		$contentFrame->content = $content;

		try {
			$this->loadDefaultContent($this->template);

			$criteria = array();
			$criteria['wantTo'] = 15;
			if ($this->isMethodPost()) {
				$criteria = $this->_profileHelper->recordFromForm();
				$criteria['nameLike'] = $this->request->post('nameLike');

				$profileService = new Service_GenericSeedniaServiceStub(WSROOT.WSCONTEXT.'/'.SERVICE_PROFILE.'/', ACCESS_TOKEN);
				$profileService->init($this->_requestContext);

				$queryParams = array('offset' => 0, 'max' => 20
					, '_commandSuffix' => '-by_user'
					, 'userSid' => (int)$this->_sessionInfo->userInfo['userSid'] );
				$searchParam = array('criteria'=>json_encode($criteria), 'params' => json_encode($queryParams));
				$content->searchResult = $profileService->get($searchParam, 'search');

				//$content->searchResult = $profileService->getList($criteria, 0, 10, 'search/');
			} else {
				$content->searchResult = null;
			}
			$content->criteria = $criteria ;

		} catch (Exception $ex)
		{
			Kohana_Log::instance()->add(Kohana_Log::ERROR, 'Exception::Add action_index: ' . $ex->getMessage());
		}
		
	}
	
	public function action_project()
	{
		$contentFrame = $this->index('Seednia::Project');
		$content = $this->createView('search_profile');
		$contentFrame->content = $content;

		
		try {
			$this->_projectHelper->loadUserProjects($this->template);

			$ref_types = array('People' => 'Profile', 'Goals' => 'Project', 'Posts' => 'Post');
			$content->ref_types = $ref_types;

			$criteria = array();
			if ($this->isMethodPost()) {

				$criteria['nameLike'] = $this->request->query('nameLike');
				$criteria['description'] = $this->request->query('description');
				$criteria['goal'] = $this->request->query('goal');
				$criteria['challenges'] = $this->request->query('challenges');
				$criteria['requiredSkillTagNamesCsv'] = $this->request->query('skills');
				$criteria['interestTagNamesCsv'] = $this->request->query('interests');

				$criteria['timeCommitment'] = $this->request->query('timeCommitment');
				$criteria['wantTo'] = $this->request->query('wantTo');

				$queryParams = array('offset'=>0, 'max'=>10);
				$searchParam = array('criteria'=>$criteria, 'params' => json_encode($queryParams));

				$content->searchResult = $this->profileService->getList($criteria, 0, 10, 'search/');
			} else {
				$content->searchResult = null;
			}
			$content->criteria = $criteria ;

		} catch (Exception $ex)
		{
			Kohana_Log::instance()->add(Kohana_Log::ERROR, 'Exception::Add action_index: ' . $ex->getMessage());
		}

		
	}
	
	
} // End Controller_Home